package admin.models;

import helpers.GeneralHelper;

import java.io.Serializable;
import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import systems.ConfigSystem;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;

import entities.SiteConfig;

import admin.models.SiteConfigModel;

public class SiteConfigModel implements Serializable{
	private Objectify ofy;
	
	private Cache cache = null;
	
	private static SiteConfigModel instance = null;
	
	public static SiteConfigModel getInstance() {
		if (instance == null) {
			instance = new SiteConfigModel();
		}
		
		return instance;
	}
	
	private SiteConfigModel() {		
		try {
			ObjectifyService.register(SiteConfig.class);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		cache = GeneralHelper.getCache();
		ofy = GeneralHelper.getObjectify();
	}
	
	public SiteConfig getConfig(long id) {
		SiteConfig config = new SiteConfig();
		
		try {
			config = ofy.get(new Key<SiteConfig>(SiteConfig.class, id));
		} catch (Exception e) {
			config = new SiteConfig();
		}
		
		return config;
	}
	
	public void insert(SiteConfig config) {
		ConfigSystem.setConfigs(config);
		ofy.put(config);
	}
	
	public void delete(SiteConfig config) {
		ofy.delete(config);
	}
}
